package com.bidevalution.business.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

public class BaseHandler extends DefaultHandler {
    private static final Logger log = LoggerFactory.getLogger(BaseHandler.class);
    private Long start;
    private String fileName = "";
    private List<String> elementPath = new CopyOnWriteArrayList<>();

    CheckHandler checkHandler;
//
//    private List<String> xmlPaths = new CopyOnWriteArrayList<>();
//    private List<Object> values = new CopyOnWriteArrayList<>();

    public BaseHandler(){

    }

    public BaseHandler(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void startDocument() throws SAXException {
        log.info("开始解析xml "+ fileName);
        start = System.currentTimeMillis();
        super.startDocument();
    }


//    public void markElement(String qname, Object obj) {
//        xmlPaths.add(qname);
//        values.add(obj);
//    }

    @Override
    public void endDocument() throws SAXException {
        log.info("结束解析xml  " +fileName +  " "+ (System.currentTimeMillis() - start));
        super.endDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//        elementPath.add()
        String lastEle = elementPath.size() > 0 ? elementPath.get(elementPath.size() -1) : null;
        if (!qName.equals(lastEle)) {
            elementPath.add(qName);
        }
        super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        String lastEle = elementPath.get(elementPath.size() -1);
        if (qName.equals(lastEle)) {
            elementPath.remove(elementPath.size() -1);
        }

//        if (xmlPaths.size() > 0) {
//            int lastInx = xmlPaths.size()-1;
//            String lastElemnt = xmlPaths.get(lastInx);
//            if (lastElemnt.equals(qName)) {
//                xmlPaths.remove(lastInx);
//                values.remove(lastInx);
//            }
//        }

        super.endElement(uri, localName, qName);
    }

//    private Object getLastElementObj() {
//        int size = values.size();
//        return size == 0 ? null : values.get(size-1);
//    }

    public List<String> getElementPath() {
        return elementPath;
    }

    /**
     * 取上一级节点名
     * @return 节点名
     */
    public String getParentName() {
        return elementPath.size() > 2 ? elementPath.get(elementPath.size() - 2) : null;
    }

    /**
     * 取上两级节点名
     * @return 节点名
     */
    public String getGrandpaName() {
        return elementPath.size() > 2 ? elementPath.get(elementPath.size() - 3) : null;
    }

    /**
     * 检查类
     */
    public interface  CheckHandler {
        Map check(Attributes attributes);
    }
}
